package md.lanjing.com.module_mine.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.lxh.baselibray.constant.ARouterConfig;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.RegexUtils;
import com.lxh.baselibray.util.SPUtils;
import com.lxh.baselibray.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

import com.lxh.baselibray.constant.Constant;

import md.lanjing.com.module_mine.R;
import md.lanjing.com.module_mine.R2;
import md.lanjing.com.module_mine.bean.PurseAddressResponce;
import md.lanjing.com.module_mine.contact.BindPurseContact;
import retrofit2.Response;
@Route(path = ARouterConfig.Mine_bind_purse_address_act)
public class BindPurseAddressActivity extends MvpActivity<BindPurseContact.BindPursePresent> implements BindPurseContact.IBindPurseView {
    @BindView(R2.id.et_purse_address)
    EditText etPurseAddress;
    @BindView(R2.id.tv_save)
    TextView tvSave;
    String purseAddress;

    @Override
    public void initData(Bundle savedInstanceState) {
        String purseAddress = SPUtils.getString(Constant.PURSE_ADDRESS, null, context);
        if (ObjectUtils.isEmpty(purseAddress)){
            //未绑定
            etPurseAddress.setHint("输入钱包地址");
            tvSave.setText("保存");

        }else {
            etPurseAddress.setText(purseAddress);
            tvSave.setText("修改");

        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_bind_purse_address;
    }


    @OnClick({R2.id.tv_save})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.tv_save) {
            String text = tvSave.getText().toString();
            if ("保存".equals(text)) {
                purseAddress = etPurseAddress.getText().toString();
                if (ObjectUtils.isEmpty(purseAddress)) {
                    ToastUtils.showLongToast(context, "请输入钱包地址");
                    return;
                }
                if (!RegexUtils.isDigitalAndLetter(purseAddress) && purseAddress.length() != 42) {
                    ToastUtils.showLongToast(context, "请输入42位字母加数字的钱包地址");
                    return;
                }
                showLoadingDialog();
                mPresent.bindPurse(context, purseAddress);
            } else {
                etPurseAddress.setHint("输入钱包地址");
                etPurseAddress.setText("");
                tvSave.setText("保存");
            }


        }
    }

    @Override
    public void getBindPurseData(Response<PurseAddressResponce> response) {
        dismissLoadingDialog();
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            ToastUtils.showLongToast(context, "绑定钱包地址成功");
            SPUtils.putString(Constant.PURSE_ADDRESS, purseAddress, context);
            finish();
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    protected BindPurseContact.BindPursePresent createPresent() {
        return new BindPurseContact.BindPursePresent();
    }

}
